/*
 * Author:          Xiaosi Xu
 * Written:         2/10/2014
 * Last Updated:    2/10/2014
 * 
 * The basic component of an order. The main product of the restaurant
 * The main product of the restaurant.
 */

package JavaSource;
import java.io.Serializable;
import java.util.*;

/**
 *
 * @author Xiaosi Xu
 * @version 1.0
 */
public class Dish implements Serializable,IdGenerable,Comparable<Dish> {
    //Attribute
    private static int globalId=3000;
    private int dishId;
    private String name;
    private double price;
    private float rating;
    private boolean status;
    private boolean availability;
    private String picture;
    /*Status includes pending, 
    in-process, ready for pick-up(delivery)
    in-transit and completed*/    
    private LinkedList<Ingredient> ingredient;
    private int ratingCount;
    private Dish menuDish;	
    private int qty;   //to record the quantity of each dish in one order
    //Constructor
    public Dish( String name, double price, float rating, boolean availability, boolean status, LinkedList<Ingredient> ingredient){
        this.dishId = generateId();
        this.name = name;
        this.price = price;
        this.rating = rating;
        this.availability = availability;
        this.status = status;
        this.ingredient = ingredient;
        this.ratingCount = 1;
    }
    public Dish( String name, double price,  boolean availability){
        this.dishId = generateId();
        this.name = name;
        this.price = price;
        this.picture = "";
        this.availability = availability;
        
        ingredient = new  LinkedList<Ingredient>();
        this.ratingCount = 1;
    }
    /**
     * This is dish shown in  order,
     * which points to menu dish
     * @param menuDish 
     */
    public Dish (Dish menuDish) {
        this.menuDish = menuDish; 
        this.qty = 1;
        this.status = true;
        this.price = menuDish.price;
    }
    public Dish () {
        this.dishId = generateId();
        ingredient = new  LinkedList<Ingredient>();
    }

    //Method
    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }    
    public static int getGlobalId() {
        return globalId;
    }

    public static void setGlobalId(int globalId) {
        Dish.globalId = globalId;
    }
    
    public int getDishId() {
        return dishId;
    }

    public void setDishId(int dishId) {
        this.dishId = dishId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price * qty;
    }
    
    public double readPrice(){
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public float getRating() {
        return rating;
    }

    /**
    * Rate the customer's order's dish,
    * and update menu dish' rating
     * @param score
    */
    public void setRating(float score) {
        this.rating = score;
        this.menuDish.ratingCount++;
        this.menuDish.rating = 
                (score+this.menuDish.rating)/(this.menuDish.ratingCount);
    }

    public String isAvailability() {
        if (this.availability )
        return "Available";
        else return "Unavailable";
    }

      public boolean isAvailability(boolean value) {
       return this.availability;
    }
    public void setAvailability(String availability) {
        if("Available".equals(availability))
            this.availability= true;
        else this.availability=false;
    }
    public void setAvailability(boolean availability) {
        this.availability = availability;
    }
   public String getDishStatus() {
        if (this.status )
        return "Pending";
        else return "Done";
    }

    public void setDishStatus(String status) {
        if("Pending".equals(status))
            this.status= true;
        else this.status=false;
    }

    public LinkedList<Ingredient> getIngredient(boolean menuOrdish) {
        return ingredient;
    }
    
    public LinkedList<Ingredient> getIngredient() {
        return this.ingredient;
    }

    public void setIngredient(LinkedList<Ingredient> ingredient) {
        this.ingredient = ingredient;
    }

    public int getRatingCount() {
        return ratingCount;
    }

    public void setRatingCount(int ratingCount) {
        this.ratingCount = ratingCount;
    }
    public int getQty() {
        return qty;
    }
    

    public void setQty(int qty) {
        this.qty = qty;
    }
    
       public LinkedList<Ingredient> getDetail(){
        return null;
    }
    
    public Dish getOrderDish() {
        return null;
    }
    
    public void updateDish(String name, double price, boolean a, String p){
        this.name = name;
        this.price = price;
        this.availability = a;
        this.picture = p;
    }
    
    public double updateDishRating(){
        return 0.0;
    }
    
    public boolean toggleAvailability(){
        return false;
    }
    
    public boolean toggleStatus(){
        return false;
    }
    
    public boolean updateStatus(int status){
        return false;
    }

    public Dish getMenuDish() {
        return menuDish;
    }

    @Override
    public int generateId() {
        return ++globalId;
    }

    @Override
    public int compareTo(Dish o) {  
        Dish other = (Dish)o;
        if(this.rating > other.rating){
             return 1;
        } else if (this.rating == other.rating) {
            return 0;
        } else {
            return -1;
        }        
    }
    
     public boolean checkDishAvailability(){
    ingredient=this.getIngredient();
    for(Ingredient i: ingredient)
    {
        if(i.checkIngredientAvailability()==false) return false;
    }
    return true && this.availability;
    }
}
